package com.bite.spring.demo.controller;

import com.bite.spring.demo.model.Person;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/r1")
    public String r1(String keyword){
        return "keyword"+keyword;
    }
    @RequestMapping("/r2")
    public String r2(String name,String password){
        return "name"+name+"password:"+password;
    }
    @RequestMapping("/r5")
    public Object r5(Person person){
        return person.toString();
    }
    @RequestMapping("/r6")
    public String r6(@RequestParam(value = "q",required = false) String keyword){
        return "keyword"+keyword;
    }
    @RequestMapping("/r7")
    public String r7(String[] arr){
        return "arr"+ Arrays.toString(arr);
    }
    @RequestMapping("/r8")
    public String r8(@RequestParam List<Integer> list){
        return "list"+list;
    }
    @RequestMapping("/r9")
    public String r9(@RequestBody Person person){
        return person.toString();
    }
    @RequestMapping("/r11/{type}/{id}")
    public String r11(@PathVariable Integer id,@PathVariable("type") String artType){
        return "获取文章id"+id+"type"+artType;
    }
    public String r12(@RequestPart("file11")MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\temp\\"+file.getOriginalFilename()));
        return "success";
    }
    @RequestMapping("/r13")
    public String r13(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie:cookies) {
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "success";
    }
    @RequestMapping("/r14")
    public String r14(@CookieValue("java") String java){
        return "从cookie获取java的value"+java;
    }
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("username","zhangsan");
        session.setAttribute("age",17);
        return "success";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session == null){
            return "用户未登录";
        }else {
            String userName = (String)session.getAttribute("userName");
            return "登录用户为"+userName;
        }
    }
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("userName") String userName){
        return userName;
    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String agent = request.getHeader("Agent");
        return "header,agent"+agent;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("Agent") String agent){
        return "agent"+agent;
    }
}
